<?php
/**
 * 华道问题件入库 
 */
class Cli_CasehuadaoissueController extends Ctrl_Cli{
	public function indexAction(){
		while(true){
            $tTime = time();
            $tRunlogdir = APPLICATION_PATH . '/shell/runlog/cli_casehuadaoissue.run';
            Tool_Fnc::writefile($tRunlogdir , $tTime);
			$this->opt();
			sleep(5);
		}
		exit;
	}
	private function opt(){
        $tCHIMO = new Case_Huadao_IssueModel;
        $tCHIDatas = $tCHIMO->field('created,case_id,content,datatype,id')->where('status = 0')->fList();

        $tUCLMO = new User_Case_LogModel;
        $tUCMO = new User_CaseModel;
        $tUCFMO = new User_Case_FailModel;
        $tCHMO = new Case_HuadaoModel;

        foreach($tCHIDatas as $tRow){
            $tTime = time();
            $tUCLMO->begin();
            $tData = array(
                'title' => '有问题',    
                'created' => $tRow['created'],
                'case_id' => $tRow['case_id'],
                'created' => $tTime,
                'status' => 5,
            );     
            if(!$tUCLMO->insert($tData)){
                $tUCLMO->back(); 
                continue;
            }


            $tData = array(
                'status' => 5, 
                'id' => $tRow['case_id'],
                'updated' => $tTime,
            );
            if(!$tUCMO->update($tData)){
                $tUCLMO->back(); 
                continue;
            }else{
				//发送短信提醒
				$tSMO = new SmsModel;
				$tUMO = new UserModel;
				$tSql = 'select u.phone from '.$tUCMO->table.' uc left join '.$tUMO->table.' u on uc.uid = u.id where uc.id = '.$tRow['case_id'];
				$tPhone = $tUCMO->query($tSql)[0]['phone'];
				$tCritcode = $tUCMO->query($tSql)[0]['critcode'];
				if(!empty($tPhone)){
					$tHCTGMO = new Huadao_Core_T_GrouppolicyinsuredModel;
					$tSTMO = new Sms_TemplateModel;
					$tPolicyno = $tHCTGMO->field('POLICYNO')->where('INSUREDIDNO = "'.$tCritcode.'"')->fRow()['POLICYNO'];
					$tMid = $tSTMO->field('tpl_id')->where('policyno = "'.$tPolicyno.'" and type = 2')->fRow()['tpl_id'];
					$tSMO->caseStatus($tPhone,$tMid);
				}
			}

            $tSql = 'update ' . $tCHIMO->table . ' set status = 1 where id = ' . $tRow['id'];
            if(!$tCHIMO->exec($tSql)){
                $tUCLMO->back(); 
                continue;
            }

            $tDatatype = explode('|',$tRow['content']);
            $tDatatypeStr = $tDatatype[0];
            $tDatatype2 = explode(',',$tDatatype[1]);

            #用户案件 问题原因
            foreach($tDatatype2 as $tDT){

                $tData = array(
                    'case_id'  => $tRow['case_id'],
                    'created' => $tTime,
                    'datatype' => $tUCMO->tDataTypeStr[$tDT],
                    'introduce' => $tDatatypeStr,
                );

                if(!$tID = $tUCFMO->insert($tData)){
                    $tUCLMO->back(); 
                    continue;
                }
                $tDatatypeStr = '';
            }

            //更新中间表状态
            $tData = array(
                'status' => 5, 
                'id' => $tRow['case_id'],
            );
            $tCHMO->update($tData);

            $tUCLMO->commit();
        }
	}
}

